/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-12-07 09:26:03
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-26 11:13:39
 * @FilePath: /yt-devops/lib/pages/devices/device_tab/device_detail/business_electricity_storage_device_detail/business_electricity_storage_device_detail_view.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/network_image_load/image_utils.dart';

import 'package:yt_devops/components/timer_picker.dart';
import 'package:yt_devops/model/device_model.dart';
import 'package:yt_devops/pages/devices/device_tab/components/child_device_widget.dart';
import 'package:yt_devops/pages/devices/device_tab/device_detail/business_electricity_storage_device_detail/chart/storage_trend_line_chart.dart';
import 'package:yt_devops/pages/devices/device_tab/device_detail/business_electricity_storage_device_detail/chart/strorage_power_line_chart.dart';
import 'package:yt_devops/pages/devices/device_tab/utils/device_util.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/site_mains_elec_detail/keep_aliver_wrapper.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/enums.dart';
import 'business_electricity_storage_device_detail_controller.dart';

class BusinessElectricityStorageDeviceDetailPage
    extends GetView<BusinessElectricityStorageDeviceDetailController> {
  final DeviceModel? deviceModel;
  const BusinessElectricityStorageDeviceDetailPage(this.deviceModel, {Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget.defaultAppBar(controller.title),
      body: SafeArea(
        child: GetBuilder<BusinessElectricityStorageDeviceDetailController>(
          initState: (state) {
            controller.deviceModel = deviceModel;
            controller.updateDeviceAddress(
                lat: deviceModel?.latitude,
                lng: deviceModel?.longitude,
                addr: deviceModel?.address,
                type: deviceModel!.mapType);
          },
          builder: (controller) => ListView(
            children: [
              Gaps.vGap10,
              marginWrap(Column(
                children: [
                  DeviceUtil.headTitle(
                    ResKey.deviceStatiscsOverview.tr,
                    padding: EdgeInsets.only(left: 16.w),
                    color: Colors.transparent,
                    height: 56,
                  ), // '数据概览'
                  Divider(
                    color: Colour.dividerColor,
                    thickness: 0.5,
                    height: 1,
                    indent: 16.w,
                    endIndent: 16.w,
                  ),
                  chartWrap(
                    null,
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        chartTitle(ResKey.essPower.tr),
                        YtTimePicker(
                          pickerMode: DateTimePickerMode.day,
                          initialDateTime: controller.day,
                          onChange: (date, timeType) =>
                              controller.powerTimeChange(date, timeType),
                        ),
                        if (controller.storagePower != null)
                          KeepAliveWrapper(
                            StoragePowerLineChart(
                              chartData: controller.storagePower!,
                              axisLabel: controller.axisLabel(60),
                            ),
                          ),
                      ],
                    ),
                  ),
                  KeepAliveWrapper(
                    chartWrap(
                      null,
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          chartTitle(ResKey.essChargeDischargeCapacity.tr),
                          YtTimePicker(
                            pickerMode: DateTimePickerMode.all,
                            initialDateTime: controller.day,
                            onChange:
                                (date, StatisticUnitStatusType? timeType) =>
                                    controller.trendTimeChange(date, timeType),
                          ),
                          if (controller.storageTrend != null)
                            StorageTrendLineChart(
                              chartData: controller.storageTrend!,
                            )
                        ],
                      ),
                    ),
                  ),
                ],
              )),
              marginWrap(ChildDeviceWidget(controller.deviceId, false)),
              if (controller.deviceAddress != null)
                marginWrap(Container(
                  constraints: BoxConstraints(
                    minHeight: 50.h,
                  ),
                  padding: EdgeInsets.symmetric(horizontal: 16.w),
                  child: Row(
                    children: [
                      Text('设备地址', style: TextStyles.f16.mediumStyle),
                      SizedBox(
                        width: 41.w,
                      ),
                      Expanded(
                        child: Text(
                            controller.deviceAddress!.formattedAddress ?? '',
                            style: TextStyles.lf14,
                            textAlign: TextAlign.right),
                      ),
                      Gaps.hGap8,
                      Image.asset(
                        'assets/images/site_base_info_location.png',
                        width: 16,
                      ),
                    ],
                  ),
                ))
            ],
          ),
        ),
      ),
    );
  }

  Widget chartWrap(double? height, Widget child) {
    return Container(
      height: height,
      child: child,
    );
  }

  Widget chartTitle(String title) {
    return Padding(
      padding: EdgeInsets.only(left: 16.w),
      child: Text(
        title,
        style: TextStyles.f16.mediumStyle.copyWith(height: 3),
      ),
    );
  }

  Widget marginWrap(Widget child) {
    return Container(
      margin: EdgeInsets.fromLTRB(16.w, 0, 16.w, 10),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        color: Colour.white,
      ),
      child: child,
    );
  }
}
